package com.rdc.jiajiadictionary.homepage;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.rdc.jiajiadictionary.R;
import com.rdc.jiajiadictionary.dailysentence.DailySentenceActivity;
import com.rdc.jiajiadictionary.notebook.NotebookActivity;
import com.rdc.jiajiadictionary.translate.TranslateActivity;


public class HomePageFragment extends Fragment implements HomePageViewInterface,View.OnClickListener {

    private HomePagePresenter homePagePresenter;
    private RelativeLayout  dailySentenceRl;
    private RelativeLayout notebookRl;
    private RelativeLayout pictureLl;
    private RelativeLayout translateRl;
    private TextView picTv;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.homepage_fragment,container,false);

        dailySentenceRl= (RelativeLayout) view.findViewById(R.id.rl_dailysentence);
        pictureLl = (RelativeLayout) view.findViewById(R.id.ll_picture);
        translateRl = (RelativeLayout) view.findViewById(R.id.rl_translate);
        notebookRl = (RelativeLayout) view.findViewById(R.id.rl_notebook);
        picTv = (TextView) view.findViewById(R.id.tv_pic);
        dailySentenceRl.setOnClickListener(this);
        translateRl.setOnClickListener(this);
        notebookRl.setOnClickListener(this);


        return view;
    }

    @Override
    public void onResume() {

        ConnectivityManager connectionManager = (ConnectivityManager)
                getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            homePagePresenter.start();
            System.out.println("1111111111111111");
        } else {
            picTv.setVisibility(View.GONE);
            System.out.println(".............");
        }
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()){

            case R.id.rl_dailysentence:
                intent = new Intent(getActivity(), DailySentenceActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_translate:
                intent = new Intent(getActivity(), TranslateActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_notebook:
                intent = new Intent(getActivity(), NotebookActivity.class);
                startActivity(intent);
        }
    }


    /**
     * 绑定presenter
     * @param presenter
     */
    @Override
    public void setPresenter(HomePagePresenter presenter) {
        homePagePresenter = presenter;
    }

    /**
     * 提供更新iu的方法给presenter
     * @param drawable
     */
    @Override
    public void setData(Drawable drawable) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                pictureLl.setBackground(drawable);
            }
        });

    }
}
